package com.inkyi.assistant.app.todolist;


import com.formdev.flatlaf.extras.FlatSVGIcon;
import com.inkyi.assistant.app.todolist.left.TodoLeftPanel;
import com.inkyi.assistant.app.todolist.right.TodoRightDrawerContent;
import com.inkyi.assistant.app.todolist.right.TodoRightMainContent;
import com.inkyi.assistant.app.todolist.right.TodoRightPanel;
import com.inkyi.assistant.app.utils.InkAppUtils;
import com.inkyi.framework.common.event.InkActionEventHelper;
import com.inkyi.framework.common.event.InkActionEventWrap;
import com.inkyi.framework.common.lang.Pair;

import javax.swing.*;
import java.util.UUID;

/**
 * 上下文
 *
 * @author lhkma
 * @date 2023/11/13
 */
public class TodoContext {

    // 组图标
    public static final Icon GROUP_ICOM_DEF = new FlatSVGIcon("icons/todolist/liebiao.svg", 14, 14);
    public static final Icon GROUP_ICOM_JINTIN = new FlatSVGIcon("icons/todolist/jintian.svg", 14, 14);
    public static final Icon GROUP_ICOM_ZHONGYAO = new FlatSVGIcon("icons/todolist/zhongyao.svg", 14, 14);
    public static final Icon GROUP_ICOM_QUANBU = new FlatSVGIcon("icons/todolist/quanbu.svg", 14, 14);
    public static final Icon GROUP_ICOM_YIWANCHENG = new FlatSVGIcon("icons/todolist/yiwancheng.svg", 14, 14);

    // 组ID
    public static final Long GROUP_ID_JINTIAN = InkAppUtils.getSnowflakeID();
    public static final Long GROUP_ID_ZHONGYAO = InkAppUtils.getSnowflakeID();
    public static final Long GROUP_ID_QUANBU = InkAppUtils.getSnowflakeID();
    public static final Long GROUP_ID_YIWANCHENG = InkAppUtils.getSnowflakeID();


    private static final Pair<Boolean, TodoListMain> todoListMain = new Pair<>(false, null);
    /**
     * 分组列表
     */
    private static final Pair<Boolean, TodoLeftPanel> todoLeftPanel = new Pair<>(false, null);
    /**
     * 待办列表
     */
    private static final Pair<Boolean, TodoRightPanel> todoRightPanel = new Pair<>(false, null);
    /**
     * 待办列表-主内容
     */
    private static final Pair<Boolean, TodoRightMainContent> todoRightMainContent = new Pair<>(false, null);
    /**
     * 待办列表-抽屉
     */
    private static final Pair<Boolean, TodoRightDrawerContent> todoRightDrawerContent = new Pair<>(false, null);
    /**
     * 事件包装
     */
    private static final InkActionEventWrap EVENT_WRAP = InkActionEventHelper.get(UUID.randomUUID().toString());


    public static <T> T getPairValue(Pair<Boolean, T> pair) {
        if (pair.getKey()) {
            return pair.getValue();
        } else {
            throw new RuntimeException("没有初始化");
        }
    }

    public static <T> void setPairValue(Pair<Boolean, T> pair, T value) {
        if (pair.getKey()) {
            return;
        }
        if (value == null) {
            throw new RuntimeException("不能为空");
        }
        pair.setKey(true);
        pair.setValue(value);
    }

    public static TodoRightPanel getTodoRightPanel() {
        return getPairValue(todoRightPanel);
    }

    public static void setTodoRightPanel(TodoRightPanel panel) {
        if (!TodoContext.todoRightPanel.getKey() && panel != null) {
            TodoContext.todoRightPanel.setKey(true);
            TodoContext.todoRightPanel.setValue(panel);
        }
    }

    public static TodoRightMainContent getTodoRightMainContent() {
        return getPairValue(todoRightMainContent);
    }

    public static void setTodoRightMainContent(TodoRightMainContent panel) {
        if (!TodoContext.todoRightMainContent.getKey() && panel != null) {
            TodoContext.todoRightMainContent.setKey(true);
            TodoContext.todoRightMainContent.setValue(panel);
        }
    }

    public static TodoRightDrawerContent getTodoRightDrawerContent() {
        return getPairValue(todoRightDrawerContent);
    }

    public static void setTodoRightDrawerContent(TodoRightDrawerContent panel) {
        if (!TodoContext.todoRightDrawerContent.getKey() && panel != null) {
            TodoContext.todoRightDrawerContent.setKey(true);
            TodoContext.todoRightDrawerContent.setValue(panel);
        }
    }

    public static TodoLeftPanel getTodoLeftPanel() {
        return getPairValue(todoLeftPanel);
    }

    public static void setTodoLeftPanel(TodoLeftPanel panel) {
        if (!TodoContext.todoLeftPanel.getKey() && panel != null) {
            TodoContext.todoLeftPanel.setKey(true);
            TodoContext.todoLeftPanel.setValue(panel);
        }
    }

    public static TodoListMain getTodoListMain() {
        return getPairValue(todoListMain);
    }

    public static void setTodoListMain(TodoListMain panel) {
        if (!TodoContext.todoListMain.getKey() && panel != null) {
            TodoContext.todoListMain.setKey(true);
            TodoContext.todoListMain.setValue(panel);
        }
    }


    /**
     * 获取事件包装
     *
     * @return {@link InkActionEventWrap }
     */
    public static InkActionEventWrap getEventWrap() {
        return EVENT_WRAP;
    }

}
